#include "qsvghost.h"
#include <QDebug>
#include "property/qabstractproperty.h"
#include "qpropertyfactory.h"
#include "../widgets/qsvgbox.h"
#include "graphlets/graphletsutil.h"
QSvgHost::QSvgHost(QAbstractHost *parent) : QFrameHost(parent) {
    setProperty("need_frame", false);
    setProperty("accept_drop", false);
    setProperty("scaledContents", true);
}

QString QSvgHost::getShowName(){
    return m_svgName;
}
QString QSvgHost::getShowIcon(){
    return m_svgIcon;
}
QString QSvgHost::getShowGroup(){
    return m_svgGroup;
}
QString QSvgHost::getSvgGuid(){
    return m_svgGuid;
}
QString QSvgHost::getSvgCode(){
    return m_svgCode;
}
void QSvgHost::setShowName(QString name){
    m_svgName = name;
}
void QSvgHost::setShowIcon(QString icon){
    m_svgIcon = icon;
}

void QSvgHost::setShowGroup(QString group){
    m_svgGroup = group;
}

void QSvgHost::setSvgGuid(QString guid){
    m_svgGuid = guid;
}

void QSvgHost::setSvgCode(QString svgCode){
    m_svgCode = svgCode;
    qDebug() << "QSvgHost::setSvgCode:svg code:" << m_svgCode;

}

void QSvgHost::onPropertyChanged(QVariant oldValue,QVariant newValue,QString propertyName){
    qDebug() << "QSvgHost::propertyChanged:setting name->" << propertyName << ",old value->" << oldValue << ",new value->" << newValue;
    emit notifyPropertyChanged(oldValue,newValue,propertyName);
}

void QSvgHost::setSvgFile(QString fileUrl){
    m_svgFile = fileUrl;
}
/*
void QSvgHost::setSvgProperties(QList<QAbstractProperty> svgProperties){
    m_svgProperties = svgProperties;
}

QList<QAbstractProperty> QSvgHost::getSvgProperties(){
    return m_svgProperties;
}
*/

void QSvgHost::initWidgetObject(){
    TagWidget* svgBox = dynamic_cast<TagWidget*>(m_object);
    Graphlets tGraph=GraphletsUtil::loadFromXml(m_svgCode);
    qDebug() << "QSvgHost::createObject->var size 1" << tGraph.m_gVars.size();
    for(int i=0;i<tGraph.m_gVars.size();i++){
        GraphletsVar gVar = tGraph.m_gVars.at(i);
        VarTag tempTag;
        qDebug() << "QSvgHost::createObject:Properties size->" << m_nameToProperty.size();
        QAbstractProperty *pro = m_nameToProperty.value(gVar.m_id);
        qDebug() << "QSvgHost::createObject:Property for id->" << gVar.m_id << ",value->" << pro->getValue();
        //QAbstractProperty *pro = this ->getProperty(gVar.m_id);
        if(pro){ //如果有设置值，则取设置值，如果没有，取初始值
            tempTag.value = pro->getValue().toString();
            if(tempTag.value.isNull()||tempTag.value.isEmpty()){
                tempTag.value = gVar.m_initialValue;
            }
        }else{
            tempTag.value = gVar.m_initialValue;
        }
        tempTag.initialValue = gVar.m_initialValue;
        tempTag.name = gVar.m_id;

        svgBox->addTag(tempTag);
    }
}

void QSvgHost::initProperty(){
    QFrameHost::initProperty();

    Graphlets graph = GraphletsUtil::loadFromXml(m_svgCode);
    QList<GraphletsVar> vList = graph.m_gVars;
    qDebug() << "QSvgHost::initProperty:var list size->" << vList.size() ;
    foreach(GraphletsVar gVar,vList){
        //其它属性
        QAbstractProperty* pObj;
        if(gVar.m_varType.isEmpty()){
           pObj = QPropertyFactory::create_property("Var");
        }else{
           pObj = QPropertyFactory::create_property(gVar.m_varType);
        }
        qDebug() << "QSvgHost::initProperty:create pro from var->" << gVar.m_varType << ",result->" << pObj << ",parent size->" << pObj->getChildren().size() ;
        if(pObj != NULL) {
            pObj->setObjectProperty("name", gVar.m_id);//需要在这里动态创建属性
            pObj->setProperty("dataType",gVar.m_dataType);
            pObj->setAttribute("show_name", gVar.m_viewName);
            pObj->setAttribute("group", gVar.m_group);
            pObj->setAttribute(ATTR_CAN_SAME, true);
            connect(pObj,&QAbstractProperty::value_chaged,this,&QSvgHost::onPropertyChanged);
            insertProperty(pObj);
        }
    }
    removeProperty("frameShape");
    removeProperty("frameShadow");
}


void QSvgHost::createObject() {
    qDebug() << "QSvgHost::createObject:svg code:" << m_svgCode;
    //初始化svg代码
    QSvgBox *svgBox = new QSvgBox();
    QString svgModuleId = m_svgGuid;
    svgModuleId.append("_");
    svgModuleId.append(getID());
    svgBox->setObjectName(svgModuleId);
    svgBox->setSvgCodeTemplate(m_svgCode);
    //初始化变量

    Graphlets tGraph=GraphletsUtil::loadFromXml(m_svgCode);
    qDebug() << "QSvgHost::createObject->var size 1" << tGraph.m_gVars.size();
    for(int i=0;i<tGraph.m_gVars.size();i++){
        GraphletsVar gVar = tGraph.m_gVars.at(i);
        VarTag tempTag;
        qDebug() << "QSvgHost::createObject:Properties size->" << m_nameToProperty.size();
        QAbstractProperty *pro = m_nameToProperty.value(gVar.m_id);
        qDebug() << "QSvgHost::createObject:Property for id->" << gVar.m_id << ",value->" << pro->getValue();
        //QAbstractProperty *pro = this ->getProperty(gVar.m_id);
        if(pro){ //如果有设置值，则取设置值，如果没有，取初始值
            tempTag.value = pro->property("value").toString();
            if(tempTag.value.isNull()||tempTag.value.isEmpty()){
                tempTag.value = gVar.m_initialValue;
            }
        }else{
            tempTag.value = gVar.m_initialValue;
        }
        tempTag.initialValue = gVar.m_initialValue;
        tempTag.name = gVar.m_id;
        svgBox->addTag(tempTag);
    }
    //连接属性变化
    connect(this,&QSvgHost::notifyPropertyChanged,svgBox,&QSvgBox::onSettingChanged);
    m_object = svgBox;
    setPropertyValue("geometry", QRect(0, 0, 200, 180));
    qDebug() << "QSvgHost::createObject:id" << svgModuleId;
}
